var MaterialConfigModel = function (data) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (data) {
      self.minHeight = data.minHeight;
      self.isGroup = data.isGroup;
      self.count = data.count;

      self.classifyConfig = data.classifyConfig;

      self.fontSize = data.fontSize || 14;
      self.lineHeight = data.lineHeight || 15;
    }
  };

  this.resetData = function (data) {
    this.minHeight = data.minHeight;
    this.isGroup = data.isGroup;
    this.count = data.count;
    this.fontSize = data.fontSize;
    this.lineHeight = data.lineHeight;

    var classifyConfig = data.classifyConfig;
    var classifyList = [];
    for (var i = 0; i < classifyConfig.length; i++) {
      var classify = classifyConfig[i];
      var columns = classify.columns;
      columns = _.map(columns, function (column) {
        return {
          id: column.id,
          width: column.width,
          isShow: column.checked
        };
      });
      classifyList.push({
        classifyName: classify.classifyName,
        columns: columns
      });
    }
    this.classifyConfig = classifyList;
  };

  this.resetWidth = function (columnKey, newWidth, oldWidth) {
    if (columnKey.indexOf('_') === -1) {
      return;
    }

    var arr = columnKey.split('_');
    var classifyConfig = this.classifyConfig;
    var classifyName = arr[0];
    var classify = _.find(classifyConfig, {
      classifyName: classifyName
    });
    if (classify === undefined) {
      return;
    }

    var columns = classify.columns;
    var obj = _.find(columns, {
      id: arr[1]
    });
    if (obj === undefined) {
      return;
    }

    var width = obj.width;
    var rangeWidth = newWidth - oldWidth;
    width = width / oldWidth * rangeWidth;
    width = _.round(width, 0);
    obj.width += width;
  };

  this.getObject = function () {
    var postData = {
      minHeight: this.minHeight,
      isGroup: this.isGroup,
      count: this.count,
      fontSize: this.fontSize,
      lineHeight: this.lineHeight
    };

    var classifyConfig = this.classifyConfig;
    var classifyList = [];
    for (var i = 0; i < classifyConfig.length; i++) {
      var obj = classifyConfig[i];
      var columns = obj.columns;
      var columnList = [];
      for (var j = 0; j < columns.length; j++) {
        var columnObj = columns[j];
        var isShow = columnObj.isShow;
        var width = columnObj.width;
        if (isShow || width !== undefined) {
          columnList.push({
            id: columnObj.id,
            isShow: isShow,
            width: width
          });
        }
      }
      classifyList.push({
        classifyName: obj.classifyName,
        columns: columnList
      });
    }
    postData.classifyConfig = classifyList;

    return postData;
  };

  fn.initData(data);
};

MaterialColorConfigModel.materialFields = [{
  id: "materialType",
  label: "物料类型"
}, {
  id: "materialName",
  label: "物料名称"
}, {
  id: "materialProviderName",
  label: "供应商"
}, {
  id: "materialModel",
  label: "规格"
}, {
  id: "materialColor",
  label: "色号"
}, {
  id: "materialColorDescription",
  label: "颜色描述"
}, {
  id: "orderTotal",
  label: "订单数"
}, {
  id: "feedTotal",
  label: "投料数"
}, {
  id: "yltsTotal",
  label: "用料数"
}, {
  id: "remark",
  label: "备注"
}];
